<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script>
</head>
<body>

    <h3>HTTP 请求范围</h3>
    <button id="down">下载</button>

    <a id="download"></a>
    <script>

        $('#down').click(function () {

            //文件大小，字节
           let size = 14088958;
           let chunkPice = 1024 * 1024 * 5;//5m

            let form = 0;
            let to = chunkPice
            let num = Math.ceil(size/chunkPice)

            let blob = [];
            for(let i = 0; i < num; i++){

                $.ajax({
                    async:false,
                    type: 'post',
                    //contentType:'application/json',
                    headers: {
                        Range: 'bytes=' + form +'-' + to
                    },
                    url: 'http://localhost:9001/download',
                    data: {
                        from:form,
                        to:to,
                        uri:"F:\\JAVA视频\\pdfBook\\JAVA核心知识点整理.pdf",
                        target:'',
                        id:'1111111'
                    },
                    success: function(data) {
                        blob.push(data);
                    },
                    error: function(err) {
                        console.log('xxxx')
                    },
                    complete: function(XMLHttpRequest, status) { //请求完成后最终执行参数　
                    }
                })


                form = to + 1;
                to= to + chunkPice;
            }

            var aBlob = new Blob( blob, {
                type: 'application/octet-stream'
            } );

            const link = document.querySelector('#download')
            link.download = 'file.pdf' // 设置文件名

            link.href = URL.createObjectURL(aBlob)
            // js主动触发下载
            document.querySelector('#download').click()
        })





    </script>
</body>
</html>